package com.dehui.traders.ui.strategy;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.HorizontalScrollView;

import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.adapter.OptionCodeAdapter;
import com.dehui.traders.adapter.TableAdapter;
import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.OptionalCodeRank;
import com.dehui.traders.bean.RealtimeQuote;
import com.dehui.traders.bean.StrategyInfo;
import com.dehui.traders.config.Config;
import com.dehui.traders.databinding.FragmentStrateDetailBinding;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.base.AppFragment;
import com.dehui.traders.ui.stockdetail.StocksDetailActivity;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by hqq on 18.3.30.
 */

public class StrateDetailFragment extends AppFragment<FragmentStrateDetailBinding,StrateDetailViewModel> implements TableAdapter.OnTableItemClick {

    public static final String IntentKey_Strate = "IntentKey_Strate";
    public static final String IntentKey_Name = "IntentKey_Name";
    private List<RealtimeQuote> quotelist = new ArrayList<>();
    private String strateName;
    private OptionCodeAdapter adapter;
    private List<OptionalCodeRank> codeRanks;
    public HorizontalScrollView mTouchView;
    private List<StrategyInfo.StrategyStock> strateList;

    @Override
    public int initContentView() {
        return R.layout.fragment_strate_detail;
    }

    @Override
    public void initParam() {
        //获取列表传入的实体
        Bundle mBundle = getArguments();
        if (mBundle != null) {
            strateList = (List<StrategyInfo.StrategyStock>) mBundle.getSerializable(IntentKey_Strate);
            strateName = mBundle.getString(IntentKey_Name);
            initStrate(strateList);
        }
    }

    private void initStrate(List<StrategyInfo.StrategyStock> strateList) {
        codeRanks = new ArrayList<>();
        for (StrategyInfo.StrategyStock item : strateList){
            String code = item.getCODE();
            OptionalCodeRank codeRank = new OptionalCodeRank(item.getNAME(),code,item.getMARKET(),0);
            codeRanks.add(codeRank);
        }
    }

    private void searchRealTime() {
        Call<BaseListResponse<RealtimeQuote>> call = NetImpl.getRealtimeQuote(codeRanks);
        call.enqueue(new CallListener<BaseListResponse<RealtimeQuote>>() {
            @Override
            public void _onResponse(Response<BaseListResponse<RealtimeQuote>> response) {
                binding.refreshView.setRefreshing(false);
                BaseListResponse<RealtimeQuote> realtimeQuoteBaseListResponse = response.body();
                if (realtimeQuoteBaseListResponse.status == 200){
                    List<RealtimeQuote> realtimeQuoteList = realtimeQuoteBaseListResponse.result;
                    updataList(realtimeQuoteList);

                }
            }

            @Override
            public void _onFailure() {
                binding.refreshView.setRefreshing(false);
            }
        });
    }

    private void updataList(List<RealtimeQuote> list) {
        quotelist.clear();
        quotelist.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public StrateDetailViewModel initViewModel() {
        return new StrateDetailViewModel(getContext());
    }

    @Override
    public void initData() {
        super.initData();
        initToolbar(strateName);
        initViews();
        binding.refreshView.setRefreshing(true);
        searchRealTime();
    }

    private void initViews() {
        adapter = new OptionCodeAdapter(getContext(), quotelist, this);
        binding.tabView.setFixes(Config.indexTitle)
                .setOthers(Config.optionOtherTitle)
                .setFragment(this)
                .setOnItemClick(this)
                .setAdapter(adapter).build();

        binding.refreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                searchRealTime();
            }
        });
    }

    @Override
    public void onItemClick(int position) {
        RealtimeQuote quote = quotelist.get(position);
        String market = strateList.get(position).getMARKET();
        StocksDetailActivity.start(getActivity(),strateName,quote.name,quote.code,String.valueOf(quote.closePrice),market);
    }


    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        List<MyHScrollView> mHScrollViews = binding.tabView.getScrollViews();
        for(MyHScrollView scrollView : mHScrollViews) {
            if(mTouchView != scrollView)
                scrollView.smoothScrollTo(l, t);
        }
    }
}
